package com.joolun.web.controller.tennis;

import java.util.List;

import com.joolun.common.core.domain.entity.SysUser;
import com.joolun.common.core.domain.model.LoginUser;
import com.joolun.common.utils.ServletUtils;
import com.joolun.common.utils.StringUtils;
import com.joolun.framework.web.service.TokenService;
import com.joolun.tennis.service.ICoachService;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.joolun.common.annotation.Log;
import com.joolun.common.core.controller.BaseController;
import com.joolun.common.core.domain.AjaxResult;
import com.joolun.common.enums.BusinessType;
import com.joolun.tennis.domain.Club;
import com.joolun.tennis.service.IClubService;
import com.joolun.common.utils.poi.ExcelUtil;
import com.joolun.common.core.page.TableDataInfo;

/**
 * 俱乐部Controller
 * 
 * @author www.joolun.com
 * @date 2022-03-08
 */
@RestController
@RequestMapping("/tennis/club")
public class ClubController extends BaseController
{
    @Autowired
    private IClubService clubService;

    @Autowired
    private ICoachService coachService;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询俱乐部列
     * 展示俱乐部名称、所在城市、LOGO、联系电话
     */
    @PreAuthorize("@ss.hasPermi('tennis:club:list')")
    @GetMapping("/list")
    public TableDataInfo list(Club club)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();
        if(!user.isAdmin()&&!user.isCmsAdmin()){
            club.setUserId(user.getUserId());
        }
        startPage();
        List<Club> list = clubService.selectClubList(club);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('tennis:club:list')")
    @GetMapping("/listByUser")
    public AjaxResult alllist(Club club)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();
        if(!user.isAdmin()&&!user.isCmsAdmin()){
            club.setUserId(user.getUserId());
        }
        List<Club> list = clubService.selectClubList(club);
        return AjaxResult.success(list);
    }

    /**
     * 导出俱乐部列表， 暂时不需要
     */
//    @PreAuthorize("@ss.hasPermi('tennis:club:export')")
//    @Log(title = "俱乐部", businessType = BusinessType.EXPORT)
//    @GetMapping("/export")
//    public AjaxResult export(Club club)
//    {
//        List<Club> list = clubService.selectClubList(club);
//        ExcelUtil<Club> util = new ExcelUtil<Club>(Club.class);
//        return util.exportExcel(list, "club");
//    }

    /**
     * 获取俱乐部详细信息
     * 获取俱乐部相关所有信息
     * - 基本信息
     * - 教练员列表
     * - 相关附件（主要是图片）
     */
    @PreAuthorize("@ss.hasPermi('tennis:club:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(clubService.selectClubById(id));
    }

    /**
     * 新增俱乐部
     */
    @PreAuthorize("@ss.hasPermi('tennis:club:add')")
    @Log(title = "新增俱乐部", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Club club)
    {
        if(StringUtils.isEmpty(club.getcName())) return AjaxResult.error("俱乐部名称必填！");
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();
        if(!user.isAdmin()&&!user.isCmsAdmin()){
            return AjaxResult.error("权限不够，请联系管理员！");
        }
        int row = clubService.insertClub(club);
        if(row==-1)return AjaxResult.error("俱乐部名称重复！");
        return toAjax(row);
    }

    /**
     * 修改俱乐部
     */
    @PreAuthorize("@ss.hasPermi('tennis:club:edit')")
    @Log(title = "俱乐部更新", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Club club)
    {
        return toAjax(clubService.updateClub(club));
    }

    /**
     * 俱乐部 教练员互相绑定
     */
    @PreAuthorize("@ss.hasPermi('tennis:club:edit')")
    @Log(title = "俱乐部绑定教练员", businessType = BusinessType.UPDATE)
    @GetMapping("bindCoach")
    public AjaxResult editCoach(@RequestParam("coachId") Long coachId,@RequestParam("clubId")Long clubId)
    {
        return toAjax(coachService.insertCoachClub(coachId,clubId));
    }

    /**
     * 俱乐部 教练员互相绑定
     */
    @PreAuthorize("@ss.hasPermi('tennis:club:edit')")
    @Log(title = "俱乐部取消绑定教练员", businessType = BusinessType.UPDATE)
    @GetMapping("cancelCoach")
    public AjaxResult cancleCoach(@RequestParam("coachId") Long coachId,@RequestParam("clubId")Long clubId)
    {
        return toAjax(coachService.cancelCoach(coachId,clubId));
    }

    /**
     * 删除俱乐部
     */
    @PreAuthorize("@ss.hasPermi('tennis:club:remove')")
    @Log(title = "删除俱乐部", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();
        if(!user.isAdmin()&&!user.isCmsAdmin()){
            return AjaxResult.error("权限不够，请联系管理员！");
        }
        return toAjax(clubService.deleteClubByIds(ids));
    }
}
